#region Using directives
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using webapp4U.BOL;
#endregion


namespace webapp4U.DAL
{
	 public class ChatlieuDAL : BaseDAL
	 {
	     #region ObjIDataReader

	     /// <summary>
	     /// Returns a new Chatlieu instance filled with the DataReader's current record data 
	     /// </summary>
	     private Chatlieu GetChatlieuFromReader(IDataReader dr)
	     {
		     Chatlieu obj = new Chatlieu();
		     obj.ChatlieuID = (dr["ChatlieuID"] is DBNull) ? Int32.MinValue :  (Int32)dr["ChatlieuID"];
		     obj.Title = (dr["Title"] is DBNull) ? String.Empty :  (String)dr["Title"];
		     obj.Title_en = (dr["Title_en"] is DBNull) ? String.Empty :  (String)dr["Title_en"];
		     obj.DisplayOrder = (dr["DisplayOrder"] is DBNull) ? 0 :  (Int32)dr["DisplayOrder"];
		     return obj;
	     }
	     #endregion

	     /// <summary>
	     /// Returns a collection of Chatlieu with the data read from the input DataReader
	     /// </summary>
	     private List<Chatlieu> GetChatlieuCollectionFromReader(IDataReader dr)
	     {
		     List<Chatlieu> obj = new List<Chatlieu>();
		     while(dr.Read())
			     obj.Add(GetChatlieuFromReader(dr));
		     return obj;
	     }

	     public int Insert(Chatlieu obj)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("w4u_Chatlieu_Insert", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@ChatlieuID", SqlDbType.Int).Direction = ParameterDirection.Output;
		     myCommand.Parameters.Add("@Title", SqlDbType.NVarChar).Value = obj.Title;
		     myCommand.Parameters.Add("@Title_en", SqlDbType.NVarChar).Value = obj.Title_en;
		     myCommand.Parameters.Add("@DisplayOrder", SqlDbType.Int).Value = obj.DisplayOrder;

		     int ret = myCommand.ExecuteNonQuery();
		     return (int)myCommand.Parameters["@ChatlieuID"].Value; 
		     }
	     }

	     public bool Update(Chatlieu obj)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("w4u_Chatlieu_Update", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@ChatlieuID", SqlDbType.Int).Value = obj.ChatlieuID;
		     myCommand.Parameters.Add("@Title", SqlDbType.NVarChar).Value = obj.Title;
		     myCommand.Parameters.Add("@Title_en", SqlDbType.NVarChar).Value = obj.Title_en;
		     myCommand.Parameters.Add("@DisplayOrder", SqlDbType.Int).Value = obj.DisplayOrder;

		     int ret = myCommand.ExecuteNonQuery();
		     return (ret == 1);
		     }
	     }

	     public bool Delete(Int32 ChatlieuID)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("w4u_Chatlieu_Delete", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@ChatlieuID", SqlDbType.Int).Value = ChatlieuID;

		     int ret = myCommand.ExecuteNonQuery();
		     return (ret == 1);
		     }
	     }

	     /// <summary>
	     /// Retrieves the w4u_Chatlieu with the specified ID
	     /// </summary>
	     public Chatlieu GetChatlieuByID(Int32 ChatlieuID)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("w4u_Chatlieu_GetChatlieuByID", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@ChatlieuID", SqlDbType.Int).Value = ChatlieuID;

		     SqlDataReader dr = myCommand.ExecuteReader();
		     return (dr.Read() ? GetChatlieuFromReader(dr) : null);
		     }
	     }

	     //Get All Chatlieu
	     public List<Chatlieu> GetAllChatlieu(int PageIndex, int PageSize)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("w4u_Chatlieu_GetAllChatlieu", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
		     myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
		     SqlDataReader dr = myCommand.ExecuteReader();
		     return GetChatlieuCollectionFromReader(dr);
		     }
	     }

	     // Get Count All records of Table  Chatlieu
	     public int GetCountChatlieu()
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("w4u_Chatlieu_GetCountChatlieu", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     return (int)myCommand.ExecuteScalar();
		     }
	     }



	 }
}
